﻿using System;
using System.IO;
using System.Windows.Forms;
using System.Xml.Serialization;
using 快速切换弹幕模式.QuickExchange;

namespace 快速切换弹幕模式
{
    public class plugin
    {
        public plugin()
        {
            
        }
        
        #region 这个是软件调用的部分，请勿阻塞或者使用消息框。
        /// <summary>
        /// 软件主界面打开后触发start
        /// </summary>
        public void start()
        {
            Config.getInstance().Load();
        }

        /// <summary>
        /// 软件关闭时触发stop
        /// </summary>
        public void stop()
        {

        }

        /// <summary>
        /// 弹幕来了触发dmshow
        /// </summary>
        public void dmshow(string danmuInfo)
        {
           
        }

        /// <summary>
        /// 主程序会分享一些行为信息给插件，方便插件开发者知道主程序在干什么
        /// P.S.新的特性都会发送记录到这里
        /// </summary>
        public void ActionLog(string danmuInfo)
        {

        }
        #endregion

        /// <summary>
        /// 在插件管理器双击插件会触发admin
        /// </summary>
        public void admin()
        {
            if (int.Parse(XmlHelper.getHelper().getAttr("InQuickStartBar")) < 1|| XmlHelper.getHelper().getAttr("InQuickStartBar")=="false")
            {
                MessageBox.Show("建议您对插件点击右键,再点添加到快捷管理面板,重启软件之后更方便您使用哦!","建议");
            }
           
            //changeMode
            if (Config.getInstance().NowMode == 0)
            {
                Config.getInstance().NowMode = 2;
                Config.getInstance().Save(null);
                new GDIMessage().show("切换到游戏模式");
            }
            else
            {
                Config.getInstance().NowMode = 0;
                Config.getInstance().Save(null);
                new GDIMessage().show("切换到桌面模式");
            }
          
            SendMSG("main.change_dm_mode;",true);
        }

        #region 核心通讯
        public event EventHandler SMSG;
        private void SendMSG(string text, bool action = false)
        {
            string _text = "{dl_title}";
            if (action)
            {
                _text = "{action}";
            }
            SMSG(new object[] { _text + text }, null);
        }

        private DanmuInfo TransToEntity(string danmuInfo)
        {
            try
            {
                XmlSerializer xmls = new XmlSerializer(typeof(DanmuInfo));
                return (DanmuInfo)xmls.Deserialize(new StringReader(danmuInfo));
            }
            catch (Exception)
            {
                return null;
            }

        }
        #endregion
    }
    public class Config : IniHelper
    {
        private static Config config=new Config();
        private int nowMode;

        public int NowMode
        {
            get { return nowMode; }
            set { nowMode = value; }
        }

        private Config()
        {

        }
        public static Config getInstance()
        {
            return config;
        }
        public override void Load()
        {
            nowMode=int.Parse(ReadIniKeys("user", "changeDMtoMusicList", path+"conf.ini"));
        }

        public override void Save(string[] Args)
        {
            WriteIniKeys("user", "changeDMtoMusicList", nowMode.ToString(), path+"conf.ini");
        }
    }

    #region UP主助手实体类    
    [Serializable]
    public class DanmuInfo
    {
        public string logtext { get; set; }
        [Obsolete]
        public string uid { get; set; }
        public string bilibiliuid { get; set; }
        public string nickname { get; set; }
        public string datetime { get; set; }
        public int admin { get; set; }
        public int member { get; set; }
        public int vip { get; set; }
        public string roomid { get; set; }
        public string extra_info { get; set; }
        public string type { get; set; }
    }
    #endregion
}
